package org.corey.share.db.service;

import org.corey.share.db.dao.BaseDao;
import org.corey.share.db.entity.Link;
import org.corey.share.db.manager.DefaultDaoFactory;
import org.corey.share.db.manager.EM;
import org.corey.share.misc.PageData;

public class FriendLinkServiceImpl implements FriendLinkService {
	private BaseDao dao = new DefaultDaoFactory().creatBaseDao();

	public boolean deleteLink(Long id) {
		if (id == null) {
			throw new IllegalArgumentException();
		}
		boolean result=false;
		EM.beginTransaction();
		Link link = (Link) dao.load(id, Link.class);
		if(link!=null){
			result = dao.remove(link);
		}
		EM.commitTransaction();
		return result;
	}

	public PageData<Link> getLinksWithPage(int pageIndex, int pageSize) {
		if (pageSize < 0 || pageIndex < 0) {
			throw new IllegalArgumentException();
		}
		PageData<Link> data = dao.findAll(pageSize, pageIndex,
				Link.class, "createdDate desc");
		return data;
	}

	public boolean modifyLink(Link link) {
		if (link == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.update(link);
		EM.commitTransaction();
		return result;
	}

	public boolean saveLink(Link newLink) {
		if (newLink == null) {
			throw new IllegalArgumentException();
		}
		EM.beginTransaction();
		boolean result = dao.save(newLink);
		EM.commitTransaction();
		return result;
	}

}
